<template>
  <div class="comment-box" :style="'height: calc(' + screenHeight + 'px - 134px);'">
    <div class="comment-list">
      <empty tips="暂无评论" v-if="!list || !list.length"/>
      <div class="comment-list-item" v-for="item in list" :key="item.id">
        <div class="comment-list-header">
          <img class="comment-list-header-head-ico" :src="item.member.avatar"/>
          <div class="comment-list-header-info">
            <div class="comment-list-header-name">
              <a href="javascript:void(0);">{{item.member.name}}</a>
            </div>
            <div class="comment-list-header-time">{{friendlyDate(item.createTime)}}</div>
          </div>
        </div>
        <div class="comment-list-body">
          <div class="comment-list-body-content">
            <div class="comment-list-content-text">
              <span>{{item.content}}</span>
            </div>
          </div>
          <div class="img-area"></div>
          <div class="comment-list-body-interaction">
            <a href="javascript:void(0);" @click="commentLike(item)" :class="{'show-active' : item.like && item.like.status}">
              <i class="el-icon-thumb interaction-icon"></i>
              <span class="interaction-text">赞 <span style="color: #cccccc;">{{item.likeCount || 0}}</span></span>
            </a>
          </div>
          <div class="comment-reply-input" v-if="item.showReplyInput">
          </div>
          <div class="reply-lists" :class="{'noExtend': !item.showReply}" v-if="item['replyList'] && item['replyList'].length">
            <template v-for="reply in item['replyList']" :key="reply.id">
              <div class="reply-item" v-if="item.showReply">
                <div class="reply-body">
                  <span class="user-nick-name">{{reply.member.name}}{{reply['toMember'] && reply['replyCommentId'] !== 0 ? '  回复  ' + reply['toMember'].name : ''}}: </span>
                  <span>{{reply.content}}</span>
                </div>
                <div class="reply-footer">
                  <span class="reply-time">{{friendlyDate(reply.createTime)}} · </span>
                  <a class="reply-like" href="javascript:void(0);" @click="replyCommentLike(reply)" :class="{'show-active' : reply.like && reply.like.status}">赞 <span style="color: #cccccc;">{{reply.likeCount || 0}}</span></a>
                </div>
                <div class="reply-input" v-if="reply.showReplyInput">
                </div>
              </div>
            </template>
            <div v-if="item.showReply" @click="item.showReply = false">
              <a class="close-more-reply" href="javascript:void(0);">
                收起回复<i class="el-icon-arrow-up"></i>
              </a>
            </div>
            <div v-else @click="item.showReply = true">
              <a class="get-more-reply" href="javascript:void(0);">
                全部{{item['replyList'].length}}条回复<i class="el-icon-arrow-down"></i>
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {ref, watch} from "vue"
  import {getCommentList, saveComment, saveReplyComment} from "../../api/comment"
  import {like} from "../../api/comment/like"
  import {friendlyDate} from "../../util/dateUtils"
  import Empty from "./empty";
  export default {
    name: "topicComment",
    components: {Empty},
    props: {
      topicId: {
        type: Number
      },
      topicType: {
        type: String
      },
      notSubmitted: {
        type: Boolean,
        default: true
      }
    },
    setup(props) {
      console.log("列表")
      let showListBox = ref(true)
      const list = ref([])
      const currentReplyCommentId = ref(0)
      const subCommentFormat = function(subList) {
        if (subList && subList.length) {
          for (let i = 0; i < subList.length; i++) {
            const comment = subList[i];
            comment.showReplyInput = false;
            if (!comment.likeCount) {
              comment.likeCount = ""
            }
          }
        }
      }
      const load = function() {
        getCommentList({topicId: props.topicId, topicType: props.topicType}, res => {
          if (res && res.list && res.list.length) {
            for (let i = 0; i < res.list.length; i++) {
              const comment = res.list[i];
              comment.showReply = currentReplyCommentId.value === comment.id;
              comment.showReplyInput = false;
              if (!comment.likeCount) {
                comment.likeCount = ""
              }
              subCommentFormat(comment["replyList"]);
            }
            list.value = res.list
          } else {
            list.value = []
          }
        })
      }
      load()
      watch(() => props.topicId, () => {
        load()
      })
      const listBoxArrow = function() {
        showListBox.value = !showListBox.value;
      }
      const showReply = function(item) {
        if (props.notSubmitted) {
          return
        }
        item.showReplyInput = !item.showReplyInput
        currentReplyCommentId.value = item.id
      }
      // 提交评论回调
      const submitCallback = function(param) {
        if (props.notSubmitted) {
          return
        }
        saveComment({
          topicId: props.topicId,
          topicType: props.topicType,
          content: param.content
        }, () => {
          load()
        })
      }
      const replySubmitCallback = function(param, parentItem) {
        if (props.notSubmitted) {
          return
        }
        if (!parentItem) {
          return
        }
        saveReplyComment({
          commentId: param.commentId,
          content: param.content,
          replyCommentId: parentItem.id,
          toMemberId: parentItem.member.id
        }, () => {
          parentItem.showReplyInput = false
          parentItem.showReply = true
          load()
        })
      }
      const commentLike = function(item) {
        if (props.notSubmitted) {
          return
        }
        like(item, "comment")
      }
      const replyCommentLike = function(item) {
        if (props.notSubmitted) {
          return
        }
        like(item, "reply_comment")
      }
      const screenHeight = ref(window.innerHeight)
      console.log(screenHeight)
      window.onresize = () => {
        return (() => {
          screenHeight.value = window.innerHeight
        })()
      }
      return {
        showListBox,
        list,
        currentReplyCommentId,
        subCommentFormat,
        friendlyDate,
        listBoxArrow,
        showReply,
        submitCallback,
        replySubmitCallback,
        commentLike,
        replyCommentLike,
        screenHeight
      }
    }
  }
</script>

<style lang="scss" scoped>
  .comment-box {
    overflow: auto;
    &::-webkit-scrollbar { width: 0 !important }
    -ms-overflow-style: none;
    overflow: -moz-scrollbars-none;
  }
  .comment-list {
    margin-top: 30px;
    .comment-list-item {
      padding-bottom: 40px;
      .comment-list-header {
        height: 48px;
        margin-bottom: 15px;
        position: relative;
        display: inline-block;
        .comment-list-header-head-ico {
          width: 48px;
          height: 48px;
          line-height: 48px;
          border-radius: 24px;
          display: inline-block;
          cursor: pointer;
          background-position: 50%;
          background-size: 100%;
        }
        .comment-list-header-info {
          display: inline-block;
          position: relative;
          top: -7px;
          margin-left: 15px;
          .comment-list-header-name {
            font-weight: 500;
            font-size: 14px;
            color: #000;
            text-align: justify;
            vertical-align: baseline;
            margin-top: 5px;
            cursor: pointer;
            a {
              color: #000;
            }
            .vip-name {
              font-size: 14px;
              color: #ebba73;
            }
            .vip-level-img {
              width: 17px;
              position: relative;
              margin-left: 9px;
              cursor: pointer;
              vertical-align: middle;
              padding-bottom: 1.5px;
              display: inline-block!important;
            }
          }
          .comment-list-header-time {
            font-size: 12px;
            color: #999;
            text-align: justify;
            vertical-align: baseline;
            margin-top: 10px;
          }
        }
      }
      .comment-list-body {
        margin-left: 63px;
        font-size: 14px;
        color: #000;
        word-wrap: break-word;
        .comment-list-body-content {
          .comment-list-content-text {

          }
        }
        .img-area {
          margin-top: 15px;
        }
        .comment-list-body-interaction {
          margin-top: 15px;
          position: relative;
          height: 20px;
          a {
            cursor: default;
            text-decoration: none;
            font-size: 12px;
            color: #999;
            margin-right: 18px;
          }
          .interaction-icon {
            color: #666;
            font-size: 14px;
            vertical-align: middle;
          }
          .interaction-text {
            margin-left: 4px;
            vertical-align: middle;
            font-size: 14px;
          }
        }
        .comment-reply-input {
          margin-top: 15px;
          display: block;
        }
        .reply-input {
          margin-bottom: 18px;
          margin-top: 12px;
        }
        .reply-lists {
          display: block;
          padding-top: 12px;
          margin-top: 15px;
          background: #fafafa;
          border-radius: 4px;
          .reply-item {
            padding-bottom: 17px;
            padding-left: 18px;
            padding-right: 18px;
            .reply-body {
              margin-bottom: 6px;
              word-wrap: break-word;
              span {
                font-size: 14px;
                line-height: 20px;
              }
              .user-nick-name {
                color: #999;
                white-space: pre;
                font-weight: 500;
              }
            }
            .reply-footer {
              height: 17px;
              span, a {
                line-height: 17px;
                font-size: 12px;
                color: #999;
              }
            }
          }
          .close-more-reply {
            width: 100%;
            font-size: 12px;
            color: $--color-primary;
            text-align: center;
            display: block;
            height: 41px;
            line-height: 41px;
            cursor: pointer;
          }
          .get-more-reply {
            margin-left: 18px;
            margin-bottom: 10px;
            display: inline-block;
            font-size: 12px;
            color: $--color-primary;
            text-align: center;
          }
        }
        .noExtend {
          display: inline-block;
          padding-right: 18px;
          padding-top: 11px;
        }
      }
    }
  }
  .show-active {
    color: $--color-primary!important;
    i {
      color: $--color-primary!important;
    }
  }
</style>
<style lang="scss">
  body {
    height: 100%;
  }
</style>
